<?php
require_once("./define/define_table.php");
require_once("./class/Individu.php");
require_once("./class/Adhesion.php");

class Adherent extends Individu{

    protected $email;
    protected $filiere;
    protected $promotion;

    public function Adherent(){
    }

    public function getEmail(){
        return $this->email;
    }

    public function getFiliere(){
        return $this->filiere;
    }

    public function getPromotion(){
        return $this->promotion;
    }
    
    public function setEmail($email){
        $this->email = $email;
    }
    
    public function setFiliere($filiere){
        $this->filiere = $filiere;
    }
    
    public function setPromotion($promo){
        $this->promotion = $promo;
    }

    public function getAllAdherent(){
        global $db;
        try{
            $sql = "SELECT id_individu, nom, prenom, email, filiere, promotion  FROM ".ADHERENT_TABLE."
					JOIN ".INDIVIDU_TABLE." USING(id_individu)";
            $sth = $db->prepare($sql);
            $sth->execute();
            PrintQuery::queryPrint($sth);
            return $sth->fetchAll(PDO::FETCH_CLASS, "Adherent");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function getAdherent($id){
        global $db;
        try{
            $sql = "SELECT id_individu, nom, prenom, email, filiere, promotion  FROM ".ADHERENT_TABLE."
           			JOIN ".INDIVIDU_TABLE." USING(id_individu)
            		WHERE id_individu = :id_individu";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_individu", $id, PDO::PARAM_INT);
            $sth->execute();
            return $sth->fetchObject("Adherent");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function addOrModify(){
        global $db;
        try{
            $sql = "SELECT COUNT(*) FROM ".ADHERENT_TABLE." WHERE id_individu = :id_individu";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
            $sth->execute();
            $nb = $sth->fetch();
          
            if($nb[0] == 0){
                $sql = "INSERT INTO ".ADHERENT_TABLE." VALUES(:id_individu, :email, :filiere, :promotion)";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":email", $this->email, PDO::PARAM_STR);
                $sth->bindValue(":filiere", $this->filiere, PDO::PARAM_STR);
                $sth->bindValue(":promotion", $this->promotion, PDO::PARAM_INT);
            }
            else{
                $sql = "UPDATE ".ADHERENT_TABLE." SET email = :email, filiere = :filiere, promotion = :promotion WHERE id_individu = :id_individu";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":email", $this->email, PDO::PARAM_STR);
                $sth->bindValue(":filiere", $this->filiere, PDO::PARAM_STR);
                $sth->bindValue(":promotion", $this->promotion, PDO::PARAM_INT);
            }
            PrintQuery::queryPrint($sth);
            return $sth->execute();
            
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }
       
    
    public function getModifyForm(){
    	$html = '<form action="?module=adherent&action=doModify" method="post">
    				<table>
    					<tr>
    						<td>'._ID_INDIVIDU_.'</td>
    						<td><input type="text" value="'.$this->id_individu.'" name="id_individu" readonly /></td>
    					</tr>
    					<tr>
    						<td>'._NOM_INDIVIDU_.'</td>
    						<td><input type="text" value="'.$this->nom.'" readonly /></td>
    					</tr>
    					<tr>
    						<td>'._PRENOM_INDIVIDU_.'</td>
    						<td><input type="text" value="'.$this->prenom.'" readonly /></td>
    					</tr>
    					<tr>
    						<td>'._EMAIL_ADHERENT_.'</td>
    						<td><input type="text" value="'.$this->email.'" name="email" /></td>
    					</tr>
    					<tr>
    						<td>'._FILIERE_ADHERENT_.'</td>
    						<td><input type="text" value="'.$this->filiere.'" name="filiere" /></td>
    					</tr>
    					<tr>
    						<td>'._PROMOTION_ADHERENT_.'</td>
    						<td><input type="text" value="'.$this->promotion.'" name="promotion" /></td>
    					</tr>
    					<tr>
    						<td colspan="2"><input type="submit" value="'._EDIT_.'"</td>
    					</tr>
    				</table>
    			</form>';
    	
    	return $html;
    }
    
    public function delete(){
    	global $db;
    	try{
    		$sql = "DELETE FROM ".ADHERENT_TABLE." WHERE id_individu = :id_individu";
    		$sth = $db->prepare($sql);
    		$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
    		$sth->execute();
    		
    		$sql = "DELETE FROM ".ADHESION_TABLE." WHERE id_individu = :id_individu";
    		$sth = $db->prepare($sql);
    		$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
    		$sth->execute();
    		PrintQuery::queryPrint($sth);
    	}catch(PDOException $e){
    		echo $e->getMessage();
    	}
    }

    public function generateSelect($id = null){
        $array = self::getAllAdherent();
        $html = '<select name="id_individu">';
        foreach($array as $i){
            $selected = ($i->getIdIndividu() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$i->getIdIndividu().'" '.$selected.'>'.$i->getNom().' '.$i->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }
    
    public function generateSelectForAssociation($id_association = null, $id = null){
        $array = Adhesion::getAllAdhesion($id_association);
        $html = '<select name="id_individu">';
        foreach($array as $i){
            $selected = ($i->getIdIndividu() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$i->getIdIndividu().'" '.$selected.'>'.self::getAdherent($i->getIdIndividu())->getNom().' '.self::getAdherent($i->getIdIndividu())->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }
}
?>
